package cn.kais.drawer.bean;

import cn.kais.drawer.DrawGenerater;
import cn.kais.drawer.UnitUtil;
import cn.kais.drawer.menu.MenuBean;
import cn.kais.drawer.unit.UnitPicture;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Map;

/**
 * 当前价格视图
 */
public class BeanCurrent {

    /**
     * 入参
     */
    public int width;
    public int height;

    public String back_ima;    // 背景图片 uri
    public String back_color;  // 背景颜色 rgba(255, 255, 255, 1)

    public JSONArray coms; // 内部子组件 Json

    public Map<String, BufferedImage> bis;
    public Graphics2D sd; // 绘制

    public BeanCurrent(Graphics2D sd, JSONObject current, int width, int height, Map<String, BufferedImage> bis) {
        this.sd = sd;
        this.width = width;
        this.height = height;
        this.back_ima = current.getString("bgImg");
        this.back_color = current.getString("bgColor");

        this.coms = current.getJSONArray("componentList");
        this.bis = bis;

        this.create();
    }

    /**
     * 绘图
     */
    private void create() {
        // 背景图片优先级 最高
        if (StringUtils.isNotBlank(back_ima)) {
            // todo 耗时久
            BufferedImage back = UnitPicture.iPictureBuffer(this.back_ima);
            this.sd.drawImage(back, 0, 0, width, height, null);
        } else if (StringUtils.isNotBlank(back_color)) {
//            todo 内部组件是否透明设置  1f 不透明 0f 透明
//            AlphaComposite comBack = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
//            sd.setComposite(comBack);
            Color color = UnitUtil.toColor(this.back_color);
            this.sd.setColor(color);
            this.sd.fillRect(0, 0, width, height);
        }

        List<JSONObject> list = sort();
        for (JSONObject com : list) {
            // 不展示的字段跳过绘图
            boolean isHide = com.getBooleanValue("isHide");
            if (isHide) continue;
            // 绘制展示字段
            String code = com.getString("type");
            MenuBean menu = MenuBean.onParseCode(code);
            switch (menu) {
                case TEXT: // 文本
                    new BeanText(sd, com);
                    break;
                case PICTURE: // 图片
                    new BeanPicture(sd, com);
                    break;
                case CODE_BAR: // 条码
                    new BeanCodeBAR(sd, com);
                    break;
                case CODE_QR: // 二维码 矩阵二维码
                    new BeanCodeQR(sd, com);
                    break;
                case NUMBER: // 数值
                    new BeanNumber(sd, com);
                    break;
                case TIME: // 时间
                    new BeanTime(sd, com);
                    break;
                case SCORE: // 评分
                    new BeanScore(sd, com);
                    break;
                case BLOCK: // 圆形 矩形
                    new BeanBlock(sd, com);
                    break;
                case LINE: // 线条
                    new BeanLine(sd, com);
                    break;
                // todo 分界线 需要特殊处理
                case AD:  // 广告位
                    new BeanAd(sd, com);
                    break;
                case VIDEO: // 视频
                    new BeanVideo(sd, com);
                    break;
                case PRICE:
                    String id = DrawGenerater.doGetPriceIdForMarketCom(com);
                    if (id != null) {
                        BufferedImage ima = null;
                        if (null != bis) ima = bis.get(id);
                        new BeanPrice(sd, com, ima);
                    } else {
                        System.out.println("营销模板中使用的价签模板信息异常！");
                    }
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 组件排序
     */
    public List<JSONObject> sort() {
        List<JSONObject> list = coms.toJavaList(JSONObject.class);
        list.sort((o1, o2) -> {
            int z1 = o1.getIntValue("zIndex");
            int z2 = o2.getIntValue("zIndex");
            return z1 - z2;
        });
        return list;
    }

}
